import { createRouter, createWebHistory } from 'vue-router'
const Layout = () => import('@/layout/index.vue')

export let constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/Login.vue')
  },
  {
    path: '/oauth/login',
    component: () => import('@/views/loginPlatform/Login.vue')
  },
  {
    path: '/sso',
    component: () => import('@/views/sso/index.vue')
  },
  {
    path: '/globe3d',
    component: () => import('@/views/globe3d/index.vue')
  },
  {
    path: '/analysis',
    component: () => import('@/views/analysis/index.vue')
  },
  {
    path: '/statistics',
    component: () => import('@/views/statistics/index.vue')
  },
  {
    path: '/addStatistics',
    component: () => import('@/views/statistics/components/addStatistics.vue')
  },
  {
    path: '/showStatistics',
    component: () => import('@/views/statistics/components/showStatistics.vue')
  },
  {
    path: '/showStatistics_interim',
    component: () => import('@/views/statistics/components/showStatistics_interim.vue')
  },
  {
    path: '/dataLink',
    component: () => import('@/views/dataLink/index.vue')
  },
  {
    path: '/dataAdd',
    component: () => import('@/views/dataLink/components/dataAdd.vue')
  },
  {
    path: '/myData',
    component: () => import('@/views/myData/index.vue')
  },
  {
    path: '/myReport',
    component: () => import('@/views/myReport/index.vue')
  },
  {
    path: '/dataSource',
    component: () => import('@/views/datasourceCenter/index.vue')
  },
  {
    path: '/codingLibrary',
    component: () => import('@/views/codingLibrary/index.vue')
  },
  {
    path: '/',
    component: Layout,
    redirect: '/analysis',
    children: [
      {
        path: 'workbench',
        component: () => import('@/views/workbench/index.vue'),
        name: 'Workbench',
        meta: {
          title: '工作台',
          svgIcon: 'workbench',
          affix: true
        }
      },
      {
        path: '/system/dictionaryValue',
        component: () => import('@/views/system/dictionary/dictionaryValue.vue'),
        name: 'DictionaryValue',
        meta: {
          title: '字典值管理',
          svgIcon: '',
          hidden: true,
          activeMenu: '/system/dictionary'
        }
      },
      {
        path: '/system/roleLink',
        component: () => import('@/views/system/role/roleLink.vue'),
        name: 'RoleLink',
        meta: {
          title: '角色链接',
          svgIcon: '',
          hidden: true,
          activeMenu: '/system/role'
        }
      }
    ]
  },
  {
    path: '/403',
    component: () => import('@/views/error/403.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      hidden: true
    },
    alias: '/:pathMatch(.*)*'
  }
]

export const asyncRoutes = [
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'System',
    permissions: ['#'],
    meta: {
      title: '系统管理',
      svgIcon: 'setting'
    }
  },
  {
    path: 'user',
    component: () => import('@/views/system/user/index.vue'),
    name: 'User',
    permissions: ['menu:sysUser'],
    meta: {
      title: '用户管理',
      svgIcon: ''
    }
  },
  {
    path: 'organization',
    component: () => import('@/views/system/organization/index.vue'),
    name: 'Organization',
    permissions: ['menu:sysOrgManager'],
    meta: {
      title: '组织管理',
      svgIcon: ''
    }
  },
  {
    path: 'log',
    component: () => import('@/views/system/log/index.vue'),
    name: 'Log',
    permissions: ['menu:sysLoginInfo'],
    meta: {
      title: '登录日志',
      svgIcon: ''
    }
  },
  {
    path: 'operLog',
      component: () => import('@/views/system/operLog/index.vue'),
    name: 'OperLog',
    permissions: ['menu:sysOperLog'],
    meta: {
    title: '操作日志',
      svgIcon: ''
     }
  },
  {
    path: 'dictionary',
    component: () => import('@/views/system/dictionary/index.vue'),
    name: 'Dictionary',
    permissions: ['menu:sysDictType'],
    meta: {
      title: '字典管理',
      svgIcon: ''
    }
  },
  {
    path: 'role',
    component: () => import('@/views/system/role/index.vue'),
    name: 'Role',
    permissions: ['menu:sysRole'],
    meta: {
      title: '角色管理',
      svgIcon: ''
    }
  },
  {
    path: 'menu',
    component: () => import('@/views/system/menu/index.vue'),
    name: 'Menu',
    permissions: ['menu:sysMenu'],
    meta: {
      title: '菜单设置',
      svgIcon: ''
    }
  },
  {
    path: 'parameter',
    component: () => import('@/views/system/parameter/index.vue'),
    name: 'Parameter',
    permissions: ['menu:sysParamSet'],
    meta: {
      title: '参数设置',
      svgIcon: ''
    }
  },
  {
  path: 'monitorServer',
    component: () => import('@/views/monitor/server/index.vue'),
    name: 'MonitorServer',
    permissions: ['menu:monitorServer'],
    meta: {
    title: '服务监控',
      svgIcon: ''
    }
  },
  {
    path: '/app',
    component: Layout,
    redirect: '/app/appManager',
    name: 'App',
    permissions: ['xtjc'],
    meta: {
      title: '系统集成',
      svgIcon: 'setting'
    }
  },
  {
    path: 'appManager',
    component: () => import('@/views/systemIntegration/appManager/index.vue'),
    name: 'AppManager',
    permissions: ['menu:sysAppManager'],
    meta: {
      title: '应用管理',
      svgIcon: ''
    }
  },
  {
    path: 'appClassify',
    component: () => import('@/views/systemIntegration/appClassify/index.vue'),
    name: 'AppClassify',
    permissions: ['menu:sysAppType'],
    meta: {
      title: '应用分类',
      svgIcon: ''
    }
  },
  {
    path: 'appManagement',
    component: () => import('@/views/system/appManagement/index.vue'),
    name: 'AppManagement',
    permissions: ['menu:appManagement'],
    meta: {
      title: '应用管理',
      svgIcon: ''
    }
  },
]

export const demoRoutes = [
  {
    path: '/demo',
    redirect: '/demo/richText',
    component: Layout,
    name: 'Demo',
    meta: {
      title: 'Demo',
      svgIcon: 'demo'
    },
    children: [
      {
        path: 'richText',
        component: () => import('@/views/demo/richText.vue'),
        name: 'RichText',
        meta: {
          title: '富文本',
          svgIcon: ''
        }
      },
      {
        path: 'upAndDown',
        component: () => import('@/views/demo/upAndDown.vue'),
        name: 'UpAndDown',
        meta: {
          title: '上传下载',
          svgIcon: ''
        }
      },
      {
        path: 'echarts',
        component: () => import('@/views/demo/echarts.vue'),
        name: 'Echarts',
        meta: {
          title: 'echarts',
          svgIcon: ''
        }
      }
    ]
  }
]

if (import.meta.env.DEV) {
  // 隐藏demo
  // constantRoutes = constantRoutes.concat(demoRoutes)
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  scrollBehavior: () => ({ top: 0 }),
  routes: constantRoutes
})

export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch (error) {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
